package easier.popup.view;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * 弹出窗口,宽度与界面一致,布局下边增加暗色背景,主要用于弹出筛选
 */
public class PopupWindowFullWidthFactory extends PopupWindowFactory {

    /**
     * @param view 显示的布局
     */
    public PopupWindowFullWidthFactory( View view ) {

        super();

        /* 使用linearLayout,第一个元素是布局,第二个元素是背景 */
        LinearLayout root = (LinearLayout) View.inflate( view.getContext(), R.layout.popup_window_full_width_container, null );
        root.addView( view, 0,
            new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT )
        );

        mWindow = new PopupWindow( root );
        onWindowCreate( mWindow );

        root.findViewById( R.id.alphaBack ).setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                mWindow.dismiss();
            }
        } );
    }

    @Override
    protected void onWindowCreate( PopupWindow window ) {

        window.setFocusable( true );
        window.setOutsideTouchable( true );
    }

    @Override
    protected void onWindowBeforeShow( PopupWindow window, View anchor ) {

        /* 高度使用WRAP_CONTENT */
        window.setWidth( LayoutParams.MATCH_PARENT );
        window.setHeight( LayoutParams.WRAP_CONTENT );
    }
}
